package org.jeecg.modules.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkTpwdCreateResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.mall.entity.TbMnAdzone;
import org.jeecg.modules.mall.entity.TbMnMaterialOptional;
import org.jeecg.modules.mall.mapper.TbMnMaterialOptionalMapper;
import org.jeecg.modules.mall.service.ITaobaoApiManager;
import org.jeecg.modules.mall.service.ITbMnAdzoneService;
import org.jeecg.modules.mall.service.ITbMnMaterialOptionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 淘宝物料
 * @Author: jeecg-boot
 * @Date: 2020-05-12
 * @Version: V1.0
 */
@Service
public class TbMnMaterialOptionalServiceImpl extends ServiceImpl<TbMnMaterialOptionalMapper, TbMnMaterialOptional> implements ITbMnMaterialOptionalService {

    @Autowired
    @Lazy(true)
    private ITbMnAdzoneService tbMnAdzoneService;

    @Autowired
    @Lazy(true)
    private ITaobaoApiManager iTaobaoApiManager;

    @Override
    public IPage<TbMnMaterialOptional> getCentreList(Page<TbMnMaterialOptional> page, TbMnMaterialOptional tbMnMaterialOptional) {
        return page.setRecords(this.baseMapper.getCentreList(page, tbMnMaterialOptional));
    }

    @Override
    public TbMnMaterialOptional queryRecommendMsg(TbMnMaterialOptional optionalDo1) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("num_iid", optionalDo1.getNumIid());

        List<TbMnMaterialOptional> list = this.baseMapper.selectList(queryWrapper);
        if (null != list && list.size() > 0) {
            optionalDo1.setJddPrice(list.get(0).getJddPrice());
        } else {
            optionalDo1.setJddPrice(optionalDo1.getTitle());
        }
        return optionalDo1;
    }

    @Override
    public IPage<TbMnMaterialOptional> getMaterialOptionalList(Page<TbMnMaterialOptional> page, TbMnMaterialOptional tbMnMaterialOptional) {

        return page.setRecords(this.baseMapper.getMaterialOptionalList(page, tbMnMaterialOptional));
    }

    @Override
    public IPage<TbMnMaterialOptional> pageChoiceness(Page<TbMnMaterialOptional> page, TbMnMaterialOptional tbMnMaterialOptional) {
        return page.setRecords(this.baseMapper.pageChoiceness(page, tbMnMaterialOptional));
    }

    @Override
    public List<TbMnMaterialOptional> getTop100() {
        return this.baseMapper.getTop100();
    }

    @Override
    public Result<?> deleteTbMnMaterialOptional(String num_iid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("num_iid", num_iid);
        TbMnMaterialOptional tbMnMaterialOptional = this.getOne(queryWrapper);
        if (null != tbMnMaterialOptional) {
            this.removeById(tbMnMaterialOptional);
        }
        return Result.ok("删除成功");
    }

    @Override
    public Result<?> getNewShareToken(String num_iid, String user_id) {
        Map<String,Object> resultMap=new HashMap<>();
        String tbkCode = "";
        try {
            TaobaoClient client = new DefaultTaobaoClient(Contents.MATERIAL_OPTIONAL_URL, Contents.appkey, Contents.secret);
            String itemUrl = "https://detail.tmall.com/item.htm?id=" + num_iid;

            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("account", user_id);
            TbMnAdzone tbMnAdzone = tbMnAdzoneService.getOne(queryWrapper);
            if (null != tbMnAdzone) {
                String adzoneId = tbMnAdzone.getAdzoneId();
                adzoneId = adzoneId.trim();
                if (adzoneId.indexOf("l") > -1) {
                    adzoneId = adzoneId.substring(0, adzoneId.length() - 1);
                }
                Long adzoned = Long.valueOf(adzoneId);
                TbkDgMaterialOptionalResponse.MapData mapData = iTaobaoApiManager.getProductByItemUrl(itemUrl, adzoned);
                if (null == mapData) {
                    return Result.ok("adzone_id不存在");
                }
                resultMap.put("detail",mapData);
                TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
                req.setText(mapData.getTitle());
                if (null != mapData.getCouponEndTime()) {
                    req.setUrl("https:" + mapData.getCouponShareUrl());
                } else {
                    req.setUrl("https:" + mapData.getUrl());
                }
                req.setLogo(mapData.getPictUrl());
                TbkTpwdCreateResponse rsp = client.execute(req);
                if ("" == rsp.getErrorCode()) {
                    tbkCode = rsp.getData().getModel();
                    resultMap.put("token",tbkCode);
                }
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return Result.ok(resultMap);
    }
}
